package stud.aistar.util;

import java.util.Random;

/**
 * *
 *
 * @ClassName :wangyu
 * @Description
 * @Author
 * @Date2019/10/9 10:47
 * @Version V1.0
 */
public class RandomUtils {

    private static final String NUMBER_CHAR = "0123456789";
    private static final String LETTER_CHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /**
     * 获取定长的随机数，只包含数字
     *
     * @param length
     *                 随机数长度
     * @return
     */
    public static String generateNumberString(int length){
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(NUMBER_CHAR.charAt(random.nextInt(NUMBER_CHAR.length())));
        }
        return sb.toString();
    }

    /**
     * 获取定长的随机数，包含大小写字母
     *
     * @param length
     *                 随机数长度
     * @return
     */
    public static String generateMixString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(LETTER_CHAR.charAt(random.nextInt(LETTER_CHAR.length())));
        }
        return sb.toString();
    }

}
